# -*- coding: utf-8 -*-


from PyQt5 import QtCore, QtGui, QtWidgets
from qfluentwidgets import BodyLabel, PrimaryPushButton, TitleLabel
import sys
from PyQt5.QtWidgets import QApplication, QFrame, QHBoxLayout, QWidget
from PyQt5.QtCore import Qt
from matplotlib.backends.backend_qtagg import FigureCanvas
from matplotlib.figure import Figure
from cal import Calculator

class Ui_ReportForm(object):
    def __init__(self):
        super().__init__()
        self.file_path = None

    def setupUi(self, ReportForm):
        ReportForm.setObjectName("ReportForm")
        ReportForm.resize(900, 700)
        # 保存按钮
        self.SavePushButton = PrimaryPushButton(ReportForm)
        self.SavePushButton.setGeometry(QtCore.QRect(680, 620, 150, 40))
        font = QtGui.QFont()
        font.setPointSize(24)
        font.setBold(False)
        font.setWeight(50)
        self.SavePushButton.setFont(font)
        self.SavePushButton.setObjectName("SavePushButton")
        # 标题
        self.TitleLabel = TitleLabel(ReportForm)
        self.TitleLabel.setGeometry(QtCore.QRect(50, 30, 200, 60))
        font = QtGui.QFont()
        font.setPointSize(36)
        font.setBold(True)
        font.setWeight(50)
        self.TitleLabel.setFont(font)
        self.TitleLabel.setObjectName("TitleLabel")
        # 上一步按钮
        self.PreviousPushButton = PrimaryPushButton(ReportForm)
        self.PreviousPushButton.setGeometry(QtCore.QRect(480, 620, 150, 40))
        font = QtGui.QFont()
        font.setPointSize(24)
        font.setBold(False)
        font.setWeight(50)
        self.PreviousPushButton.setFont(font)
        self.PreviousPushButton.setObjectName("PreviousPushButton")
        #  报告内容
        self.BodyLabel_10 = BodyLabel(ReportForm)
        self.BodyLabel_10.setGeometry(QtCore.QRect(190, 580, 98, 19))
        self.BodyLabel_10.setAlignment(QtCore.Qt.AlignCenter)
        self.BodyLabel_10.setObjectName("BodyLabel_10")
        self.BodyLabel_11 = BodyLabel(ReportForm)
        self.BodyLabel_11.setGeometry(QtCore.QRect(590, 580, 98, 19))
        self.BodyLabel_11.setAlignment(QtCore.Qt.AlignCenter)
        self.BodyLabel_11.setObjectName("BodyLabel_11")

        self.layoutWidget = QtWidgets.QWidget(ReportForm)
        self.layoutWidget.setGeometry(QtCore.QRect(60, 110, 771, 151))
        self.layoutWidget.setObjectName("layoutWidget")

        self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.F15BodyLabel = BodyLabel(self.layoutWidget)
        self.F15BodyLabel.setText("")
        self.F15BodyLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.F15BodyLabel.setObjectName("F15BodyLabel")
        self.gridLayout.addWidget(self.F15BodyLabel, 2, 3, 1, 1)
        self.BodyLabel_7 = BodyLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(False)
        font.setWeight(50)
        self.BodyLabel_7.setFont(font)
        self.BodyLabel_7.setAlignment(QtCore.Qt.AlignCenter)
        self.BodyLabel_7.setObjectName("BodyLabel_7")
        self.gridLayout.addWidget(self.BodyLabel_7, 3, 0, 1, 1)
        self.A5BodyLabel = BodyLabel(self.layoutWidget)
        self.A5BodyLabel.setText("")
        self.A5BodyLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.A5BodyLabel.setObjectName("A5BodyLabel")
        self.gridLayout.addWidget(self.A5BodyLabel, 2, 6, 1, 1)
        self.F15BodyLabel_2 = BodyLabel(self.layoutWidget)
        self.F15BodyLabel_2.setText("")
        self.F15BodyLabel_2.setAlignment(QtCore.Qt.AlignCenter)
        self.F15BodyLabel_2.setObjectName("F15BodyLabel_2")
        self.gridLayout.addWidget(self.F15BodyLabel_2, 3, 3, 1, 1)
        self.F20BodyLabel_2 = BodyLabel(self.layoutWidget)
        self.F20BodyLabel_2.setText("")
        self.F20BodyLabel_2.setAlignment(QtCore.Qt.AlignCenter)
        self.F20BodyLabel_2.setObjectName("F20BodyLabel_2")
        self.gridLayout.addWidget(self.F20BodyLabel_2, 3, 4, 1, 1)
        self.BodyLabel_4 = BodyLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        font.setBold(False)
        font.setItalic(True)
        font.setWeight(50)
        self.BodyLabel_4.setFont(font)
        self.BodyLabel_4.setAlignment(QtCore.Qt.AlignCenter)
        self.BodyLabel_4.setObjectName("BodyLabel_4")
        self.gridLayout.addWidget(self.BodyLabel_4, 0, 5, 1, 1)
        self.A3BodyLabel = BodyLabel(self.layoutWidget)
        self.A3BodyLabel.setText("")
        self.A3BodyLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.A3BodyLabel.setObjectName("A3BodyLabel")
        self.gridLayout.addWidget(self.A3BodyLabel, 2, 5, 1, 1)
        self.BodyLabel_3 = BodyLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        font.setBold(False)
        font.setItalic(True)
        font.setWeight(50)
        self.BodyLabel_3.setFont(font)
        self.BodyLabel_3.setAlignment(QtCore.Qt.AlignCenter)
        self.BodyLabel_3.setObjectName("BodyLabel_3")
        self.gridLayout.addWidget(self.BodyLabel_3, 0, 4, 1, 1)
        self.F10BodyLabel_2 = BodyLabel(self.layoutWidget)
        self.F10BodyLabel_2.setText("")
        self.F10BodyLabel_2.setAlignment(QtCore.Qt.AlignCenter)
        self.F10BodyLabel_2.setObjectName("F10BodyLabel_2")
        self.gridLayout.addWidget(self.F10BodyLabel_2, 3, 2, 1, 1)
        self.BodyLabel_2 = BodyLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        font.setBold(False)
        font.setItalic(True)
        font.setWeight(50)
        self.BodyLabel_2.setFont(font)
        self.BodyLabel_2.setAlignment(QtCore.Qt.AlignCenter)
        self.BodyLabel_2.setObjectName("BodyLabel_2")
        self.gridLayout.addWidget(self.BodyLabel_2, 0, 3, 1, 1)
        self.BodyLabel_5 = BodyLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        font.setBold(False)
        font.setItalic(True)
        font.setWeight(50)
        self.BodyLabel_5.setFont(font)
        self.BodyLabel_5.setAlignment(QtCore.Qt.AlignCenter)
        self.BodyLabel_5.setObjectName("BodyLabel_5")
        self.gridLayout.addWidget(self.BodyLabel_5, 0, 6, 1, 1)
        self.line = QtWidgets.QFrame(self.layoutWidget)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.gridLayout.addWidget(self.line, 1, 0, 1, 7)
        self.F20BodyLabel = BodyLabel(self.layoutWidget)
        self.F20BodyLabel.setText("")
        self.F20BodyLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.F20BodyLabel.setObjectName("F20BodyLabel")
        self.gridLayout.addWidget(self.F20BodyLabel, 2, 4, 1, 1)
        self.BodyLabel = BodyLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(18)
        font.setBold(False)
        font.setItalic(True)
        font.setWeight(50)
        self.BodyLabel.setFont(font)
        self.BodyLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.BodyLabel.setObjectName("BodyLabel")
        self.gridLayout.addWidget(self.BodyLabel, 0, 2, 1, 1)
        self.A5BodyLabel_2 = BodyLabel(self.layoutWidget)
        self.A5BodyLabel_2.setText("")
        self.A5BodyLabel_2.setAlignment(QtCore.Qt.AlignCenter)
        self.A5BodyLabel_2.setObjectName("A5BodyLabel_2")
        self.gridLayout.addWidget(self.A5BodyLabel_2, 3, 6, 1, 1)
        self.line_2 = QtWidgets.QFrame(self.layoutWidget)
        self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        self.gridLayout.addWidget(self.line_2, 2, 1, 2, 1)
        self.F10BodyLabel = BodyLabel(self.layoutWidget)
        self.F10BodyLabel.setText("")
        self.F10BodyLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.F10BodyLabel.setObjectName("F10BodyLabel")
        self.gridLayout.addWidget(self.F10BodyLabel, 2, 2, 1, 1)
        self.A3BodyLabel_2 = BodyLabel(self.layoutWidget)
        self.A3BodyLabel_2.setText("")
        self.A3BodyLabel_2.setAlignment(QtCore.Qt.AlignCenter)
        self.A3BodyLabel_2.setObjectName("A3BodyLabel_2")
        self.gridLayout.addWidget(self.A3BodyLabel_2, 3, 5, 1, 1)
        self.BodyLabel_6 = BodyLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(False)
        font.setWeight(50)
        self.BodyLabel_6.setFont(font)
        self.BodyLabel_6.setAlignment(QtCore.Qt.AlignCenter)
        self.BodyLabel_6.setObjectName("BodyLabel_6")
        self.gridLayout.addWidget(self.BodyLabel_6, 2, 0, 1, 1)
        self.line_3 = QtWidgets.QFrame(self.layoutWidget)
        self.line_3.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_3.setObjectName("line_3")
        self.gridLayout.addWidget(self.line_3, 0, 1, 1, 1)
        # 
        self.widget = QtWidgets.QWidget(ReportForm)
        self.widget.setGeometry(QtCore.QRect(61, 271, 771, 311))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setSpacing(10)
        self.horizontalLayout.setObjectName("horizontalLayout")
        # 插两张直方图
        figure_heng = Figure(figsize=(5, 4), dpi=100)
        self.canvas_heng = FigureCanvas(figure_heng)
        self.canvas_heng.setObjectName("canvas_heng")
        self.horizontalLayout.addWidget(self.canvas_heng)
        figure_zong = Figure(figsize=(5, 4), dpi=100)
        self.canvas_zong = FigureCanvas(figure_zong)
        self.canvas_zong.setObjectName("canvas_zong")
        self.horizontalLayout.addWidget(self.canvas_zong)

        self.retranslateUi(ReportForm)
        QtCore.QMetaObject.connectSlotsByName(ReportForm)

        #self.SavePushButton.clicked.connect(self.result)

    def retranslateUi(self, ReportForm):
        _translate = QtCore.QCoreApplication.translate
        ReportForm.setWindowTitle(_translate("ReportForm", "Form"))
        self.SavePushButton.setText(_translate("ReportForm", "保存结果"))
        self.TitleLabel.setText(_translate("ReportForm", "报告"))
        self.PreviousPushButton.setText(_translate("ReportForm", "上一步"))
        self.BodyLabel_10.setText(_translate("ReportForm", "横向间距分布图"))
        self.BodyLabel_11.setText(_translate("ReportForm", "纵向间距分布图"))
        self.BodyLabel_7.setText(_translate("ReportForm", "纵向"))
        self.BodyLabel_4.setText(_translate("ReportForm", "A3"))
        self.BodyLabel_3.setText(_translate("ReportForm", "f20"))
        self.BodyLabel_2.setText(_translate("ReportForm", "f15"))
        self.BodyLabel_5.setText(_translate("ReportForm", "A5"))
        self.BodyLabel.setText(_translate("ReportForm", "f10"))
        self.BodyLabel_6.setText(_translate("ReportForm", "横向"))

    def result(self):
        if self.file_path:
            pass
        else:
            cal = Calculator(file_path=self.file_path)
            cal.calculate()
            self.data_heng = cal.data_heng
            self.data_zong = cal.data_zong

            self.A3BodyLabel.setText(str(cal.A3_zong))
            self.A5BodyLabel.setText(str(cal.A5_zong))
            self.F10BodyLabel.setText(str(cal.f10_zong))
            self.F20BodyLabel.setText(str(cal.f20_zong))
            self.F15BodyLabel.setText(str(cal.f15_zong))

            self.A3BodyLabel_2.setText(str(cal.A3_heng))
            self.A5BodyLabel_2.setText(str(cal.A5_heng))
            self.F10BodyLabel_2.setText(str(cal.f10_heng))
            self.F20BodyLabel_2.setText(str(cal.f20_heng))
            self.F15BodyLabel_2.setText(str(cal.f15_heng))

            self.plot_heng = self.canvas_heng.figure.subplots()
            self.plot_heng.hist(self.data_heng, bins=10, color='blue', alpha=0.5)
            

            self.plot_zong = self.canvas_zong.figure.subplots()
            self.plot_zong.hist(self.data_zong, bins=10, color='red', alpha=0.5)
            self.plot_zong.figure.canvas.draw()
            self.plot_heng.figure.canvas.draw()

if __name__ == '__main__':
    # enable dpi scale
    QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
    App = QApplication(sys.argv)    # 创建QApplication对象，作为GUI主程序入口
    w = QWidget()      # 实例化QMainWindow类
    aw = Ui_ReportForm()  # ()  # 创建主窗体对象，实例化Ui_MainWindow
    aw.setupUi(w)         # 主窗体对象调用setupUi方法，对QMainWindow对象进行设置
    
    w.show()               # 显示主窗体
    sys.exit(App.exec_())  # 运行主程序，并退出